package com.ytc.login.controller;

import com.ytc.user.model.User;
import com.ytc.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserService userService;
    //主页
    @RequestMapping("zhuye")
    public String getZhuye(Model model){
        return "zhuye";
    }

    //主页
    @RequestMapping("index")
    public String getMenu(Model model){
        return "index";
    }
    //前往登录页
    @RequestMapping("toLogin")
    public String toLogin(Model model){
        return "login";
    }
    //登陆
    @RequestMapping("login")
    @ResponseBody
    public String getLogin(User user, HttpServletRequest request){
        System.out.println(user.toString());
        User userRet = userService.queryUserByName(user.getUserName());
        if(userRet == null){
            System.out.println("45");
            return "2";
        }
        String pass = userRet.getUserPass();
        String pass2 = user.getUserPass();
        if(!pass2.equals(pass)){
            System.out.println("3");
            return "3";
        }
        System.out.println(userRet.toString());
        request.getSession().setAttribute("user",userRet);

        return "success";
    }
    @RequestMapping("toRegister")
    public String toRegister(Integer role,HttpServletRequest request){
        System.out.println("role:"+role);
        request.getSession().setAttribute("role1",role);
        return "register/userRegister";
    }

}
